package com.customization.assets.formmode.remind;

import com.customization.assets.util.AbstractCronJob;
import com.customization.assets.util.AssetRemind;
import com.customization.assets.util.DBTools;
import com.customization.assets.util.DateTools;
import com.customization.commons.LogTool;
import weaver.conn.RecordSet;
import weaver.soa.workflow.request.RequestService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Huangwm
 * @description 针对登记时关联了关保资产的实物，检测所关联的关保资产状态是否为报废状态或者报损报废中，并且自动发更新资产状态为已报废，并通知责任人 在使用
 */
public class AssteRetirementRemind extends AbstractCronJob {
    private LogTool log = new LogTool("/log/assetlog/huangwm/AssteRetirementRemind",false);
    private DateTools dateTools = new DateTools();
    private DBTools dbTools = new DBTools();
    @Override
    public void start() throws Exception {
        log.writeLog("检查资产状态开始");
        List<String> zrrList = getZrrList();
        List<String> bmList = getZBmList();
        /*
        if(zrrList.size()>0){
            Map<String,List<Map<String,String>>> retirementAsstes = getRetirementAsstes(zrrList);
            for (int i=0;i<zrrList.size();i++){
                String thisZrr = zrrList.get(i);
                List<Map<String,String>> thisDatas = retirementAsstes.get(thisZrr);
                String swidList = "",gbidList = "";
                String dybm = "";
                for (int j=0;j<thisDatas.size();j++) {
                    swidList += thisDatas.get(j).get("swid")+",";
                    String gbid = thisDatas.get(j).get("gbid")+",";
                    dybm = thisDatas.get(j).get("dybm");
                }
                String bmgyl = dbTools.getAssetsAdmin_bm(dybm);//获取部门管理员
                //发送提醒
                try {
                    String title = "实物资产关联关保报废状态提醒 "+dateTools.getCurrentDate();
                    Map<String,String> mainData = new HashMap<>();
                    mainData.put("xgzc", swidList);
                    mainData.put("txr", "1");
                    mainData.put("tzr", bmgyl);
                    mainData.put("bz","系统自动检测到相关资产中的实物资产所关联的关保资产状态为报损报废;<br /> 系统已自动变更实物资产状态为报废！");
                    mainData.put("xglc", "-1");
                    mainData.put("xgbm", dybm);
                    AssetRemind assetRemind = new AssetRemind();
                    RequestService service = new RequestService();
                    String requestid = service.createRequest(assetRemind.setRequestInfo("1",title,mainData));
                    log.writeLog("提醒发送成功;requestid="+requestid);
                } catch (Exception e) {
                    e.printStackTrace();
                    log.writeLog("发送提醒异常  "+e.getMessage());
                }
            }
        }else{
            log.writeLog("AssteRetirementRemind获取责任人集合为空 结束本次提醒");
        }
        */
        if(bmList.size()>0){
            RecordSet rs = new RecordSet();
            for(int i=0;i<bmList.size();i++){
                String dybm = bmList.get(i);
                String bmgly = dbTools.getAssetsAdmin_bm(dybm);
                List<Map<String,String>> bmzcList = getRetirementAsstesByBm1(dybm);
                List<String> ids = getRetirementAsstesByBm2(dybm);
                if(!"".equals(bmgly)){
                    String sql = "update uf_swzczmxx set zczt=7,zrr='',dybm='',syks='' where id in("+ids.toString().replace("[", "").replace("]","")+")";
                    log.writeLog("部门id:"+dybm+">>"+sql);
                    boolean flag = rs.execute(sql);
                    if(flag){
                        try {
                            String title = "实物资产关联关保报废状态提醒 "+dateTools.getCurrentDate();
                            Map<String,String> mainData = new HashMap<>();
                            mainData.put("xgzc", ids.toString().replace("[", "").replace("]",""));
                            mainData.put("txr", "1");
                            mainData.put("tzr", bmgly);
                            mainData.put("bz","系统自动检测到相关资产中的实物资产所关联的关保资产状态为报损报废;<br /> 系统已自动变更实物资产状态为报废！");
                            mainData.put("xglc", "-1");
                            mainData.put("xgbm", dybm);
                            AssetRemind assetRemind = new AssetRemind();
                            RequestService service = new RequestService();
                            String requestid = service.createRequest(assetRemind.setRequestInfo("1",title,mainData));
                            log.writeLog("提醒发送成功;requestid="+requestid);
                        } catch (Exception e) {
                            e.printStackTrace();
                            log.writeLog("发送提醒异常  "+e.getMessage());
                        }
                    }
                }
            }
        }else{
            log.writeLog("AssteRetirementRemind 获取资产数据为空 结束本次提醒");
        }
        log.writeLog("检查资产状态结束");
    }

    /**
     * 查询实物关联关保,且关保资产状态为报废状态，实物资产不为报废的数据（不包含报损报废中） 报废=6 报损报废中=9
     * @return
     */
    public Map<String,List<Map<String,String>>> getRetirementAsstes(List<String> zrrList){
        Map<String,List<Map<String,String>>> datas = new HashMap<String,List<Map<String,String>>>();
        if(zrrList.size()==0) return datas;
        //String sql = " select uf_swzczmxx.id as swid,uf_gbxtzczmxx.id as gbid,xzgbxtzc,kpwybh,uf_gbxtzczmxx.kph,kpzt,zczt,swzcbh,zclx,ppss,ggxhss,zrr,syks,dybm from uf_swzczmxx left join uf_gbxtzczmxx on  uf_swzczmxx.xzgbxtzc=uf_gbxtzczmxx.kpwybs  where xzgbxtzc<>'' and kpzt=6 and zczt<>6 ";
        String sql = "select uf_swzczmxx.id as swid,uf_gbxtzczmxx.id as gbid,xzgbxtzc,kpwybh,uf_gbxtzczmxx.kph,kpzt,zczt,swzcbh,zclx,ppss,ggxhss,zrr,syks,dybm  from uf_gbxtzczmxx left join uf_swzczmxx on  uf_gbxtzczmxx.kpwybs=uf_swzczmxx.xzgbxtzc where kpzt=6 and glswzc<>'' and zczt not in(2,7) ";
        log.writeLog("getRetirementAsstes-->>>"+sql);
        RecordSet rs2 = new RecordSet();
        rs2.execute(sql);
        while (rs2.next()){
            String zrr = rs2.getString("zrr");
            List<Map<String,String>> mapList = new ArrayList<>();
            for (int i=0;i<zrrList.size();i++){
                if(zrr.equals(zrrList.get(i))){
                    Map<String,String> data = new HashMap<>();
                    data.put("swid",rs2.getString("swid"));
                    data.put("gbid",rs2.getString("gbid"));
                    data.put("kpwybh",rs2.getString("xzgbxtzc"));
                    data.put("kph",rs2.getString("kph"));
                    data.put("swzcbh",rs2.getString("swzcbh"));
                    data.put("zrr",rs2.getString("zrr"));
                    data.put("syks",rs2.getString("syks"));
                    data.put("dybm",rs2.getString("dybm"));
                    data.put("pp",rs2.getString("ppss"));
                    data.put("ggxh",rs2.getString("ggxhss"));
                    mapList.add(data);
                }
            }
            //按责任人汇总对应名下的资产
            datas.put(zrr,mapList);
        }

        return datas;
    }
    /**
     * 根据部门查询实物关联关保,且关保资产状态为报废状态，实物资产不为报废的数据（不包含报损报废中） 报废=6 报损报废中=9
     * @return
     */
    public List<Map<String,String>> getRetirementAsstesByBm1(String dybm){
        List<Map<String,String>> datas = new ArrayList<>();
        //String sql = " select uf_swzczmxx.id as swid,uf_gbxtzczmxx.id as gbid,xzgbxtzc,kpwybh,uf_gbxtzczmxx.kph,kpzt,zczt,swzcbh,zclx,ppss,ggxhss,zrr,syks,dybm from uf_swzczmxx left join uf_gbxtzczmxx on  uf_swzczmxx.xzgbxtzc=uf_gbxtzczmxx.kpwybs  where xzgbxtzc<>'' and kpzt=6 and zczt<>6 ";
        String sql = "select uf_swzczmxx.id as swid,uf_gbxtzczmxx.id as gbid,xzgbxtzc,kpwybh,uf_gbxtzczmxx.kph,kpzt,zczt,swzcbh,zclx,ppss,ggxhss,zrr,syks,dybm  from uf_gbxtzczmxx left join uf_swzczmxx on  uf_gbxtzczmxx.kpwybs=uf_swzczmxx.xzgbxtzc where kpzt=6 and glswzc<>'' and zczt not in(2,7) \n" +
                "and dybm="+dybm;
        log.writeLog("getRetirementAsstesByBm-->>>"+sql);
        RecordSet rs2 = new RecordSet();
        rs2.execute(sql);
        while (rs2.next()){
            Map<String,String> data = new HashMap<>();
            data.put("swid",rs2.getString("swid"));
            data.put("gbid",rs2.getString("gbid"));
            data.put("kpwybh",rs2.getString("xzgbxtzc"));
            data.put("kph",rs2.getString("kph"));
            data.put("swzcbh",rs2.getString("swzcbh"));
            data.put("zrr",rs2.getString("zrr"));
            data.put("syks",rs2.getString("syks"));
            data.put("dybm",rs2.getString("dybm"));
            data.put("pp",rs2.getString("ppss"));
            data.put("ggxh",rs2.getString("ggxhss"));
            datas.add(data);
        }
        return datas;
    }

    /**
     * 根据部门查询实物关联关保,且关保资产状态为报废状态，实物资产不为报废的数据（不包含报损报废中） 报废=6 报损报废中=9
     * @return
     */
    public List<String> getRetirementAsstesByBm2(String dybm){
        List<String> datas = new ArrayList<>();
        //String sql = " select uf_swzczmxx.id as swid,uf_gbxtzczmxx.id as gbid,xzgbxtzc,kpwybh,uf_gbxtzczmxx.kph,kpzt,zczt,swzcbh,zclx,ppss,ggxhss,zrr,syks,dybm from uf_swzczmxx left join uf_gbxtzczmxx on  uf_swzczmxx.xzgbxtzc=uf_gbxtzczmxx.kpwybs  where xzgbxtzc<>'' and kpzt=6 and zczt<>6 ";
        String sql = "select uf_swzczmxx.id as swid,uf_gbxtzczmxx.id as gbid,xzgbxtzc,kpwybh,uf_gbxtzczmxx.kph,kpzt,zczt,swzcbh,zclx,ppss,ggxhss,zrr,syks,dybm  from uf_gbxtzczmxx left join uf_swzczmxx on  uf_gbxtzczmxx.kpwybs=uf_swzczmxx.xzgbxtzc where kpzt=6 and glswzc<>'' and zczt not in(2,7) \n" +
                "and dybm="+dybm;
        log.writeLog("getRetirementAsstesByBm-->>>"+sql);
        RecordSet rs2 = new RecordSet();
        rs2.execute(sql);
        while (rs2.next()){
            datas.add(rs2.getString("swid"));
        }
        return datas;
    }

    /**
     * 获取责任人集合
     * @return
     */
    public List<String> getZrrList(){
        RecordSet rs = new RecordSet();
        List<String> zrrList = new ArrayList<>();
        String sql = "select zrr,syks,dybm  from uf_gbxtzczmxx left join uf_swzczmxx on  uf_gbxtzczmxx.kpwybs=uf_swzczmxx.xzgbxtzc where kpzt=6 and glswzc<>''  and zczt not in(2,7) ";
        log.writeLog("getZrrList-->>>"+sql);
        rs.execute(sql);
        while (rs.next()){
            String zrr = rs.getString("zrr");
            if(!zrrList.contains(zrr)) {
                zrrList.add(zrr);
            }
        }
        return zrrList;
    }

    /**
     * 获取责任人集合
     * @return
     */
    public List<String> getZBmList(){
        RecordSet rs = new RecordSet();
        List<String> bmlist = new ArrayList<>();
        String sql = "select dybm,count(uf_swzczmxx.id) as counts  from uf_gbxtzczmxx left join uf_swzczmxx on  uf_gbxtzczmxx.kpwybs=uf_swzczmxx.xzgbxtzc where kpzt=6 and glswzc<>'' and zczt not in(2,7) \n" +
                "group by dybm";
        log.writeLog("getZBmList-->>>"+sql);
        rs.execute(sql);
        while (rs.next()){
            String zrr = rs.getString("dybm");
            if(!bmlist.contains(zrr)) {
                bmlist.add(zrr);
            }
        }
        return bmlist;
    }

}
